<template>
	<view class="realPage">
		<image src="/static/user/ws.png" mode="aspectFit" class="wsImg"></image>
		<view class="wsText">
			<text>轻</text>
			<text>松</text>
			<text>赊</text>
			<text>货</text>
		</view>
		<view class="container">
			<view class="cTitle">
				信息认证
			</view>
			<view class="list">
				<view class="listItem" @click="toAuthentication">
					<image src="/static/user/realName.png" mode=""></image>
					<text class="text">身份认证</text>
					{{isFlag ==true?'已认证':'未认证'}}
					<uni-icons type="forward"></uni-icons>
				</view>
				<view class="listItem" @click="toRealShop">
					<image src="/static/user/realShop.png" mode=""></image>
					<text class="text">商铺认证</text>
					{{CompanyInfo.CardPhoto?'已认证':'未认证'}}
					<uni-icons type="forward"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},
				CompanyInfo: {},
				// 是否认证
				isFlag: false,
				token:""
			}
		},
		async onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
			// 获取toekn
			this.getToken()
      // 获取是否认证
			this.AuthRealName()
			// 获取公司信息
			this.$http("post", "/Base_Manage/Base_Company/GetDataCompanyData", {
					Search: {
						condition: "CreatorId",
						keyword: this.userInfo.Id,
					},
				})
				.then((res) => {
					if (res.Success && res.Data.length > 0) {
						this.CompanyInfo = res.Data[0];
						uni.setStorageSync('CompanyInfo', this.CompanyInfo)
					}
				});
		},
		methods: {
			// 获取TOKEN
			async getToken() {
				await uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/token?phone=' + this.userInfo.UserPhone,
					method: 'GET'
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						this.token = JSON.parse(res.data.data).access_token
					}
				})
			},
			// 验证是否实名认证
			AuthRealName() {
				this.isFlag = false
				uni.request({
					url: this.$veriUrl + '/auth/VeriRecord/state?Value=' + this.userInfo.UserPhone,
					method: 'GET'
				}).then(result => {
					let [err, res] = result;
					if (res.data.code == 200) {
						// 说明已认证
						this.isFlag = true
						this.img = res.data.data.veriType
						// 相关字段插入用户信息  从新缓存 
						this.$set(this.userInfo, 'fromDevice', res.data.data.fromDevice)
						this.$set(this.userInfo, 'id', res.data.data.id)
						this.$set(this.userInfo, 'createTime', res.data.data.createTime)
						this.$set(this.userInfo, 'IdCard', res.data.data.idNumber)
						this.$set(this.userInfo, 'RealName', res.data.data.name)
						this.$set(this.userInfo, 'UserName', res.data.data.name)
						this.$set(this.userInfo, 'OpenId', res.data.data.openId)
						this.$set(this.userInfo, 'UserPhone', res.data.data.phone)
						this.$set(this.userInfo, 'remark', res.data.data.remark)
						this.$set(this.userInfo, 'Sign', res.data.data.sign)
						this.$set(this.userInfo, 'state', res.data.data.state)
						this.$set(this.userInfo, 'veriType', res.data.data.veriType)
						// 暂时不删除
						// this.userInfo = {
						// 	...res.data.data,
						// 	name: res.data.data.name || this.userInfo.name,
						// 	phone: res.data.data.phone || this.userInfo.phone,
						// 	idCard: res.data.data.idNumber || this.userInfo.idCard,
						// 	avatarUrl: res.data.data.avatarUrl || this.userInfo.avatarUrl,
						// 	smscode: res.data.data.smscode || this.userInfo.smscode,
						// }
						if (this.userInfo.RealName) this.getBG(this.userInfo.RealName)
						else uni.removeStorageSync('nameBG')
					} else {
						// 说明未认证
						this.isFlag = false
					}
					uni.removeStorageSync('userInfo')
					// 重新缓存用户信息
					uni.setStorageSync('userInfo', this.userInfo)
					// 存储缓存是否认证
					uni.removeStorageSync('flag', this.isFlag)
					uni.setStorageSync('flag', this.isFlag)
				})
				return this.isFlag
			},
			getBG(name) {
				this.$http('post', '/Base_Manage/Upload/GetNameSign?Name=' + name).then(res => {
					if (res.Success) uni.setStorageSync('nameBG', res.Data)
				})
			},
			// 商铺认证
			toRealShop() {
				let obj = JSON.stringify(this.CompanyInfo)
				uni.navigateTo({
					url: '/pages/User/RealShop?CompanyInfo=' + obj
				})
			},
			// 实名认证
			toAuthentication() {
				// 默认二要素
				uni.setStorageSync('type', 0)
				uni.navigateTo({
					url: '/pages/User/RealName?flag='+this.isFlag +'&token='+this.token
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.realPage {
		padding: 40rpx;
		width: 100%;
		height: 100%;
		background: $LoginBG no-repeat center center fixed;
		// background-image: $LoginBG;
		background-size: cover;
		box-sizing: border-box;
		font-family: '仿宋';

		.wsImg {
			margin-top: 120rpx;
			width: 50%;
			height: 220rpx;
			display: block;
		}

		.wsText {
			color: #3164faba;
			font-size: 18px;

			text {
				margin-right: 24rpx;
			}
		}

		.container {
			margin-top: 100rpx;

			.cTitle {
				text-align: center;
				color: #3164faba;
				font-size: 24px;

			}
		}

		.list {
			margin-top: 50rpx;

			.listItem {
				display: flex;
				align-items: center;
				background-color: #fff;
				padding: 12px 10px;
				border-radius: 10px;
				box-sizing: border-box;
				box-shadow: 0px 0px 3px #eee;
				margin-bottom: 8px;

				image {
					width: 30px;
					height: 30px;
				}

				.text {
					padding-left: 10px;
					flex: 1;
				}

				.uni-icons {
					font-weight: bold;
				}
			}
		}
	}
</style>